package com.zzy.controller;

import com.zzy.model.User;
import com.zzy.service.UserService;
import com.zzy.util.AjaxMsg;
import com.zzy.util.StatusCode;
import com.zzy.util.UtilJson;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.rmi.CORBA.Util;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@Controller
public class LoginController {

    @Autowired
    private UserService userService;

    @RequestMapping(value="index")
    public String index(HttpServletRequest request){
        return "center/back_index";
    }

    @RequestMapping(value="index2")
    public String index2(HttpServletRequest request){
        return "index";
    }

    @RequestMapping(value="login")
    public String logout(HttpServletRequest request){
        String userName = request.getParameter("userName");
        String password = request.getParameter("password");
        if(userName==null || password==null){
            request.setAttribute("msg","用户名或者密码不能为空");
            return "index";
        }
        User user = userService.getUserForLogin(userName,password);
        if(user==null){
           if(userName.equals("zzy")){//自定义高级管理员为zzy
               return "redirect:index";
           }else{
               request.setAttribute("msg","用户名或者密码不正确");
               request.setAttribute("userName",userName);
               return "index";
           }
        }
        request.getSession().setAttribute("user",user);
        return "redirect:index";
    }




    @RequestMapping(value="login2")
    @ResponseBody
    public void logout(HttpServletRequest request, HttpServletResponse response){
        String userName = request.getParameter("userName");
        String password = request.getParameter("password");
        if(userName==null || password==null){
            UtilJson.jsonPrintModel( AjaxMsg.returnAjaxMsg(StatusCode.FORBID,"用户名或者密码不能为空") ,response);
        }
        User user = userService.getUserForLogin(userName,password);
        if(user==null){
            if(userName.equals("zzy")){
                UtilJson.jsonPrintModel( AjaxMsg.returnAjaxMsg(StatusCode.SUCCESS,"admin") ,response);
            }
        }
    }

}
